//
//  UIViewController+ShowAlertController.m
//  Community
//
//  Created by Air on 2016/11/7.
//  Copyright © 2016年 MaryKayChina. All rights reserved.
//

#import "UIViewController+ShowAlertController.h"

@implementation UIViewController (ShowAlertController)

#pragma mark - UIAlertController Message Method

/**
 * 显示消息并确认关闭
 */
- (void)showAlertMessage:(NSString *)message withTitle:(NSString *)title {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title
                                                                   message:message
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"确定", nil)
                                                     style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction *action) {
                                                       
                                                   }];
    [alert addAction:action];
    
    [self presentViewController:alert animated:YES completion:^{
        
    }];
}

/**
 * 显示多个Action操作的消息
 */
- (void)showAlertMessage:(NSString *)message withTitle:(NSString *)title withActions:(NSArray *)actions {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title
                                                                   message:message
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    if (actions) {
        for (UIAlertAction *action in actions) {
            [alert addAction:action];
        }
    }
    
    [self presentViewController:alert animated:YES completion:^{
        
    }];
}

- (void)showActionSheetMessage:(NSString *)message withTitle:(NSString *)title withActions:(NSArray *)actions {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title
                                                                   message:message
                                                            preferredStyle:UIAlertControllerStyleActionSheet];
    
    if (actions) {
        for (UIAlertAction *action in actions) {
            [alert addAction:action];
        }
    }
    
    [self presentViewController:alert animated:YES completion:^{
        
    }];
}

/**
 * 显示红色文字确认按钮的消息并确认关闭
 */
- (void)showRedActionAlertMessage:(NSString *)message withTitle:(NSString *)title {
    [self showAlertMessage:message
                 withTitle:title
               withActions:@[
                             [UIAlertAction actionWithTitle:NSLocalizedString(@"确定", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
    }]
                             ]];
}

/**
 * 显示消息框并在指定迟续时间后自动关闭
 *
 * @param message           消息内容
 * @param duration          消息显示迟续时间
 */
- (void)showAlertMessage:(NSString *)message withDuration:(CGFloat)duration {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
                                                                   message:message
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    [self presentViewController:alert animated:YES completion:^{
        
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [alert dismissViewControllerAnimated:YES completion:^{
            
        }];
    });
}

/**
 * 显示消息框并在指定迟续时间后自动关闭，同时处理回调操作
 *
 * @param message           消息内容
 * @param duration          消息显示迟续时间
 * @param finishBlock       回调操作
 */
- (void)showAlertMessage:(NSString *)message withDuration:(CGFloat)duration withFinishBlock:(FinishBlock)finishBlock {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
                                                                   message:message
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    [self presentViewController:alert animated:YES completion:^{
        
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [alert dismissViewControllerAnimated:YES completion:^{
            finishBlock();
        }];
    });
}


- (void)showAlertMessage:(NSString *)message
               withTitle:(NSString *)title
             cancelTitle:(NSString *)cancelTitle
                 okTitle:(NSString *)okTitle
             cancelBlock:(FinishBlock)cancelBlock
                 okBlock:(FinishBlock)okBlock
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title
                                                                   message:message
                                                            preferredStyle:UIAlertControllerStyleAlert];
    if (cancelTitle) {
        UIAlertAction *act1 = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (cancelBlock) {
                cancelBlock();
            }
        }];
        [alert addAction:act1];
    }
    
    if (okTitle) {
        UIAlertAction *act2 = [UIAlertAction actionWithTitle:okTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (okBlock) {
                okBlock();
            }
        }];
        [alert addAction:act2];
    }
    [self presentViewController:alert animated:YES completion:NULL];
}

@end
